package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/*
* 菜品管理
* */
@Slf4j
@RestController("adminDishController")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /*
    * 新增菜品
    * */
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增的菜品信息:{}",dishDTO.toString());
        dishService.saveWithFlavor(dishDTO);

        //删除redis缓存
        String key = "dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

    /*
    * 分页查询
    * */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /*
    * 批量删除菜品
    * */
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") List<Long> ids){
        log.info("批量删除的ids:{}",ids);
        dishService.deletebatch(ids);

        //清除redis中所有分类下的菜品信息
        cleanCache("dish_*");

        return Result.success();
    }

    /*
    * 根据ID来回写数据
    * */
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据ID查询菜品:{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /*
    * 根据ID修改菜品信息和菜品口味
    * */
    @PutMapping
    public Result updateById(@RequestBody DishDTO dishDTO){
        log.info("根据id修改菜品信息:{}",dishDTO);
        dishService.updateByIdWithFlavor(dishDTO);

        //清除redis中所有分类下的菜品信息
        cleanCache("dish_*");

        return Result.success();
    }

    /*
    * 菜品启用禁用
    * */
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,long id){
        log.info("菜品启用禁用:{},{}",status,id);
        dishService.startOrStop(status,id);

        //清除redis中所有分类下的菜品信息
        cleanCache("dish_*");
        return Result.success();
    }

    /*
    * 根据分类id查询菜品信息
    * */
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据id查询菜品信息：{}",categoryId);
       List<Dish> dishList =  dishService.getList(categoryId);
        return Result.success(dishList);
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
